package cn.yunhe.video.controller;

import cn.yunhe.video.Service.*;
import cn.yunhe.video.entity.*;
import cn.yunhe.video.util.TransformDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class VideoController {
    @Autowired
    private IVideoService videoService;

    @Autowired
    private IDanmakuService danmakuService;


    @Autowired
    private IGreatService greatService;

    @Autowired
    private ICollectService collectService;


    @Autowired
    private ICoinService coinService;

    @RequestMapping(value = "/queryVideoById")
    public String query(Model model, HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        Video video = videoService.query(id);
        List<Danmup> danmupList = danmakuService.query(id);
        List<Danmup> danmups = new ArrayList<>();

        Video video1 = new Video();
        video1.setId(id);

        UserInfo userInfo =  new UserInfo();
        userInfo.setUid(1);

        Great great = new Great();
        great.setUserInfo(userInfo);
        great.setVideo(video1);

        Coin coin = new Coin();
        coin.setUserInfo(userInfo);
        coin.setVideo(video1);

        Coin coin1 = coinService.query(coin);
        if(coin1!=null){
            request.setAttribute("coin","已投币");
        }else{
            request.setAttribute("coin","投币");
        }



        Great great1 = greatService.queryGreat(great);
        if(great1!=null){
            request.setAttribute("great","取消点赞");
        }else{
            request.setAttribute("great","点赞");
        }







        if (collectService.getCollectByName(1) != null) {
            model.addAttribute("msg","取消收藏");
        } else {
            model.addAttribute("msg","收藏");
        }


        for (Danmup d : danmupList) {
            Danmup danmup = new Danmup();
            String time = TransformDate.formatSecondDateTime(Integer.parseInt(d.getTime()));
            danmup.setCode(d.getCode());
            danmup.setText(d.getText());
            danmup.setSize(d.getSize());
            danmup.setColor(d.getColor());
            danmup.setPosition(d.getPosition());
            danmup.setVideo(d.getVideo());
            danmup.setTime(time);
            danmups.add(danmup);
            model.addAttribute("video", video);
            request.setAttribute("danmups", danmups);
        }
        return "demo";
    }
}
